﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace synjones.project.cas
{
    public class UserModel
    {
        public string id { get; set; }
        public string addr { get; set; }
        public string mobile { get; set; }
        public string name { get; set; }
        public string name_pinyin { get; set; }
        public string name_py { get; set; }
        public string email { get; set; }
        public string idno { get; set; }
        public string sno { get; set; }
        public string career { get; set; }
        public string nick_name { get; set; }
        public string cardno { get; set; }
        public string login_pwd { get; set; }
        public string bank_card { get; set; }
        public string classx { get; set; }
        public string grade { get; set; }
        public string specialist { get; set; }
        public string dept { get; set; }
        public string sex { get; set; }
        public string unicode { get; set; }
        public string honest { get; set; }
        public string levelx { get; set; }
        public string build_date { get; set; }
    }
    public class UserLogin
    {
        private static string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
        private static string loginid_name = System.Configuration.ConfigurationManager.AppSettings["loginid"];
        public static UserModel Login(string login_id, string password)
        {
            string sqlstr = "select * from [user] where login_pwd =@password and " + loginid_name + "=@loginid";
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sqlstr,conn);
                cmd.Parameters.Add(new SqlParameter("@password", password));
                cmd.Parameters.Add(new SqlParameter("@loginid", login_id));
                SqlDataReader sdr = cmd.ExecuteReader();
                if (sdr.HasRows)
                {
                    sdr.Read();
                    UserModel user = new UserModel();
                    for (int i = 0; i < sdr.FieldCount; i++)
                    {
                        string name = sdr.GetName(i);
                        string value= Convert.ToString(sdr.GetValue(i));
                        user.SetPropertyValue(name, value);
                    }
                    return user;
                }
                else
                {
                    return null;
                }                
            }
        }
    }
}
